package com.yanqu.road.dao.impl.activity.ghost;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.ghost.GhostAttackRecord;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GhostAttackRecordDaoImpl extends TempDataObjectDao<GhostAttackRecord> {

    public static String getGhostAttackRecordSql = "select * from t_c_activity_ghost_attack_record where activityId=? and batchNo=?";

    public Map<Integer, Map<Long, GhostAttackRecord>> getGhostAttackRecordMap(int activityId, int batchNo) {
        Map<Integer, Map<Long, GhostAttackRecord>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGhostAttackRecordSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, batchNo);
            rs = call.executeQuery();
            while (rs.next()) {
                GhostAttackRecord ghostAttackRecord = parse(rs);

                if (!map.containsKey(ghostAttackRecord.getBossId())) {
                    map.put(ghostAttackRecord.getBossId(), new ConcurrentHashMap<>());
                }

                map.get(ghostAttackRecord.getBossId()).put(ghostAttackRecord.getUserId(), ghostAttackRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateGhostAttackRecordSql = "update t_c_activity_ghost_attack_record set userBaseInfo=?,damage=?,updateTime=? where activityId=? and batchNo=? and bossId=? and serverId=? and userId=?";

    @Override
    protected boolean updateData(GhostAttackRecord ghostAttackRecord) {
        ghostAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getUpdateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getBossId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getUserId()));
        if (getDb().executeNoneQuery(updateGhostAttackRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGhostAttackRecordSql = "insert into t_c_activity_ghost_attack_record(activityId,batchNo,bossId,serverId,userId,userBaseInfo,damage,updateTime) values (?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GhostAttackRecord ghostAttackRecord) {
        ghostAttackRecord.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getBatchNo()));
        para.put(i++, new DbParameter(Types.INTEGER, ghostAttackRecord.getBossId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(ghostAttackRecord.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, ghostAttackRecord.getUpdateTime()));
        if (getDb().executeNoneQuery(insertGhostAttackRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GhostAttackRecord parseData(ResultSet rs) throws SQLException {
        GhostAttackRecord ghostAttackRecord = new GhostAttackRecord();
        ghostAttackRecord.setActivityId(rs.getInt("activityId"));
        ghostAttackRecord.setBatchNo(rs.getInt("batchNo"));
        ghostAttackRecord.setBossId(rs.getInt("bossId"));
        ghostAttackRecord.setServerId(rs.getLong("serverId"));
        ghostAttackRecord.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        ghostAttackRecord.setUserBaseInfo(StringUtils.isNullOrEmpty(userBaseInfo) ? new UserBaseInfo() : JSONObject.parseObject(userBaseInfo, new TypeReference<UserBaseInfo>() {
        }));
        ghostAttackRecord.setDamage(rs.getLong("damage"));
        ghostAttackRecord.setUpdateTime(rs.getLong("updateTime"));

        return ghostAttackRecord;
    }
}
